package com.school.controller;

import com.school.domain.User;
import com.school.extra.base.BaseController;
import com.school.extra.context.Constant;
import com.school.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import zhibi.commons.exception.MessageException;

@Controller
public class IndexController extends BaseController {

    @Autowired
    private UserService userService;


    @GetMapping("login")
    public String login() {

        return "login";
    }


    /**
     * 首页
     *
     * @param model
     * @return
     */
    @RequestMapping({"/", "index"})
    public String index(Model model) {
        model.addAttribute("tipMsg", session.getAttribute("tipMsg"));
        session.removeAttribute("tipMsg");
        return "index";
    }

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("login")
    public String login(String username, String password) {
        User user = userService.login(username, password);
        if (null == user) {
            request.setAttribute("msg", "用户名或者密码错误");
            return "login";
        }
        if (user.getStatus() == 0) {
            request.setAttribute("msg", "用户不可用");
            return "login";
        }
        session.setAttribute("sessionAdmin", user);
        return "redirect:index";
    }


    @RequestMapping("logout")
    public String logout() {
        session.removeAttribute(Constant.SESSION.ADMIN);
        return redirect("index");
    }

    @RequestMapping("modify")
    public String modify() {
        return "modify";
    }

    /**
     * 修改密码
     *
     * @param pass
     * @param newpass
     * @param confimpass
     * @return
     */
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public String modify(String pass, String newpass, String confimpass) {
        if (!newpass.equals(confimpass)) {
            throw new MessageException("两次密码不一样");
        }
        if (!sessionUser().getPassword().equalsIgnoreCase(pass)) {
            throw new MessageException("原密码错误");
        }
        User user = userService.selectByPrimaryKey(sessionUser().getId());
        user.setPassword(newpass);
        userService.updateByPrimaryKey(user);
        return redirect("logout");
    }

    /**
     * 到注册页面
     *
     * @return
     */
    @GetMapping("register")
    public String register() {
        return "register";
    }

    /**
     * 注册
     *
     * @param user
     * @param model
     * @return
     */
    @PostMapping("register")
    public String register(User user, Model model, String password2) {
        if (!user.getPassword().equals(password2)) {
            throw new MessageException("两次密码不一样");
        }
        User temp = new User();
        temp.setUsername(user.getUsername());
        int size = userService.select(temp).size();
        if (size > 0) {
            throw new MessageException("该用户名已经存在");
        }
        user.setStatus(1);
        user.setType("user");
        userService.insertSelective(user);
        return "login";
    }
}
